#!/usr/bin/env python
# coding=utf-8
from wsgiref import simple_server
from . import wsgiserver
import argparse, logging, imp

def serve(args):
	parser = argparse.ArgumentParser()
	parser.add_argument('-b', '--bind', default='0.0.0.0:80')
	#parser.add_argument('--fcgi', action='store_true')
	parser.add_argument('app')
	args = parser.parse_args(args)
	host, _, port = args.bind.partition(':')
	fp = open(args.app, 'rb')
	app = imp.load_module('wsgi_app', fp, args.app, ('.app', 'rb', imp.PY_SOURCE))
	fp.close()
	server = simple_server.make_server(
		host,
		int(port),
		app.application,
		wsgiserver.HTTPServer,
		wsgiserver.HTTPHandler
	)
	logging.info('Serving HTTP on %s, port %d', *server.server_address[:2])
	server.serve_forever()
